package com.fenghongzhang.day011;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.Telephony;
import android.util.Log;
import android.view.View;

public class SMSActivity extends AppCompatActivity {
    ContentResolver contentResolver;
    private static final String TAG = "SMSActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_s_m_s);

        //解析者,读写短信
         contentResolver = getContentResolver();


         //1, 是否要动态请求
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            requestPermissions(new String[]{Manifest.permission.READ_SMS,Manifest.permission.READ_EXTERNAL_STORAGE},888);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 888 && grantResults[0] == PackageManager.PERMISSION_GRANTED){

        }else{
            finish();
        }
    }

    public void querySms(View view) {
        Log.i(TAG, "querySms: 000");
        Cursor cursor = contentResolver.query(Telephony.Sms.CONTENT_URI, null, null, null, null);
        if(cursor!= null){
            Log.i(TAG, "querySms: 111");
            while(cursor.moveToNext()){ //移动游标 false
                String address = cursor.getString(cursor.getColumnIndex(Telephony.Sms.ADDRESS));
                String body = cursor.getString(cursor.getColumnIndex(Telephony.Sms.BODY));
                Log.i(TAG, "querySms: "+address+body);
            }
            cursor.close();
        }
    }

    public void queryPic(View view) {
        Cursor cursor = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
        if(cursor!= null){
            Log.i(TAG, "querySms: 111");
            while(cursor.moveToNext()){ //移动游标 false
                String name = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.TITLE));
                String data = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                Log.i(TAG, "querySms: "+name+data);
            }
            cursor.close();
        }
    }

    public void queryMusic(View view) {
        Cursor cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
        if(cursor!= null){
            while(cursor.moveToNext()){ //移动游标 false
                String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
                String duration = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
                String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                Log.i(TAG, "query: "+name+duration+data);
            }
            cursor.close();
        }

    }
}
